#-*-coding:utf-8-*-
import requests
import json
from bs4 import BeautifulSoup
import random

"""负责爬取学信网四六级成绩"""

# 虚假ip列表
spoof_ip_li = ['198.177.174.1', '198.19.82.28', '183.27.113.194',
               '119.151.29.113', '182.11.18.227', '18.13.118.120',
               '223.12.208.62', '219.19.154.45', '183.22.120.79']

url = "http://www.chsi.com.cn/cet/query"


def get_raw_result(zkzh, xm):
    """获得结果页面"""
    spoof_ip = random.choice(spoof_ip_li)  # 随机获取虚假ip

    # 请求头，Referer表示请求从何而来，否则学信网将拒绝请求
    headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
               'Accept-Encoding': 'gzip, deflate, sdch',
               'Accept-Language': 'zh-CN,zh;q=0.8',
               'Connection': 'keep-alive',
               'Host': 'www.chsi.com.cn',
               'Referer': 'http://www.chsi.com.cn/cet/',
               'Upgrade-Insecure-Requests': '1',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
               'X-Forwarded-For': spoof_ip}
    params = {'zkzh': zkzh,  # 参数
              'xm': xm}
    try:
        response = requests.get(
            url, params=params, headers=headers, timeout=10)
    except:  # 请求失败
        return None
    response.encoding = 'utf-8'
    return response.text


def cet_result(zkzh, xm):
    """使用BS4对得到的数据进行筛选抓取
       如果查询成功，返回结果列表，否则返回None"""
    raw_data = get_raw_result(zkzh, xm)
    if raw_data:
        soup = BeautifulSoup(
            raw_data, 'lxml', from_encoding='utf-8')  # 使用lxml引擎进行解析
        # 找到class为cetTable的table
        cettable = (soup.find_all('table', class_="cetTable"))
        if len(cettable) > 0:  # 若存在信息table
            cet_info = (cettable[0]).find_all('td')
            more = (cet_info[5].get_text("|", strip=True)).split("|")
            xm = cet_info[0].get_text(strip=True)  # 姓名
            xx = cet_info[1].get_text(strip=True)  # 学校
            kslb = cet_info[2].get_text(strip=True)  # 考试类别
            zkzh = cet_info[3].get_text(strip=True)  # 准考证号
            kssj = cet_info[4].get_text(strip=True)  # 考试时间
            zf = more[0]  # 总分
            tl = more[2]  # 听力
            yd = more[4]  # 阅读
            xzfy = more[6]  # 写作与翻译
            data = [xm, xx, kslb, zkzh, kssj, zf, tl, yd, xzfy]
            return data
        else:  # 不存在该考生
            return None
    else:
        return "QueryFail"
